#include "../inc/main.h"
#include "../inc/queue.h"


// 从文件加载患者信息

void loadPatientsFromFile(PatientQueue *queue, const char *filename)
{
    FILE *file = fopen(filename, "r");
    if (!file)
    {
        perror("无法打开文件");
        return;
    }

    char line[NAME_LENGTH];
    while (fgets(line, sizeof(line), file))
    {
        // 创建新的患者节点
        Patient *patient = (Patient *)malloc(sizeof(Patient));
        if (patient == NULL)
        {
            perror("内存分配失败");
            fclose(file);
            return;
        }

        // 去掉换行符
        line[strcspn(line, "\n")] = 0;

        // 确保名字不超过 NAME_LENGTH
        if (strlen(line) >= NAME_LENGTH)
        {
            fprintf(stderr, "患者名字过长: %s\n", line);
            free(patient);
            continue; // 跳过这个患者
        }

        strcpy(patient->name, line);
        enqueue(queue, patient);
    }

    fclose(file);
}
